<?php



class AnswerAction extends ApiAction {
    public function top() {
        $qId = I('get.qid');
        $wxuid=I('get.wxuid');
        import('ORG.Util.Page');// 导入分页类
        $map = array();
        $AnswerDb = D('Answer');



        $map['tp_answer.qId'] = $qId;
        $field='tp_member.nickname, tp_answer.aId,content,date,tp_answer.likeQty,tp_answer.collectionQty,tp_answer.wxUid,answer,push,qId,(select dlike from tp_likelog where tp_likelog.aId=tp_answer.aId and tp_likelog.wxUid='.$wxuid.') as dlike,headimgurl';

        $list = $AnswerDb->join('left join tp_wxuser on tp_wxuser.wxUid=tp_answer.wxUid')
            ->join('left join tp_member on tp_member.member_id=tp_wxuser.member_id')
            ->field($field)->where($map)->group('tp_member.nickname, tp_answer.aId,content,date,tp_answer.likeQty,tp_answer.collectionQty,tp_answer.wxUid,answer,push,qId')->order('push desc,likeQty desc')->select();
        $index=0;
        $list2=[];
        $DiscussDb = D('Discuss');
        foreach($list as $val) {
            $val['discuss']=$DiscussDb
               ->join('left join tp_wxuser on tp_wxuser.wxUid=tp_discuss.wxUid')
               ->join('left join tp_member on tp_member.member_id=tp_wxuser.member_id') 
               ->field('tp_discuss.*,nickname,headimgurl,(select dlike from tp_dlikelog where tp_dlikelog.did=tp_discuss.dId and tp_dlikelog.wxUid='.$wxuid.') as isLike,(select count(1) from tp_dlikelog where tp_dlikelog.dId=tp_discuss.did) qty')
               ->where(array('aId'=>$val['aId']))->order('tp_discuss.date')->select();
  
            $list2[$index]=$val;
            $index=$index+1;
        } 
        $ret['result'] = 'ok';
        $ret['answer'] = $list2;
        echo json_encode($ret);
    }

    public function Detail() {
        $qId = I('get.qId');

        $map = array();
        $AnswerDb = D('Answer');

        $map['tp_answer.qId'] = $qId;

        $list = $AnswerDb->join("LEFT JOIN tp_discuss ON tp_discuss.aId=tp_answer.aId")->where($map)->select();
        $ret['result'] = 'ok';
        $ret['qId'] = $qId;
        $ret['answer'] = $list;
        echo json_encode($ret);
    }

    public  function Add(){

        try{
            //throw new \think\Exception('异常消息', 100006);
            $data['content']=I('get.content');
            $data['likeQty']=0;
            $data['collectionQty']=0;
            $data['answer']='';
            $data['push']=0;
            $data['qId']=I('get.qid');
            $data['date']=date("Y-m-d H:i:s");
            $data['wxUid']=I('get.wxuId'); 
            $AnswerDb = D('Answer');

            $aid = $AnswerDb->data($data)->add();

            $q = D('Question')->where(array('qId'=>I('get.qid')))->find();
            $u = D('Wxuser')->join('join tp_member on tp_wxuser.member_id=tp_member.member_id')->where(array('wxUid'=>I('get.wxuId')))->find();
            $ms=$u['nickname'].' 回答了你的问题（'.$q['appeal'].'）';
            D('Message')->data(array('message'=>$ms,'qid'=>I('get.qid'),'aid'=>$aid,'readed'=>0,'date'=>date("Y-m-d H:i:s"),'wxuid'=>I('get.wxuId'),'doWxuid'=>$q['wxUserId'],'formId'=>I("get.formId")))->add();

            D('Question')->where(array('qId'=>I('get.qid')))->data(array('answerCount'=>$q['answerCount']+1))->save();
            //send_weapp_msg($touser,$template_id,$page,$form_id,$data,$emphasis_keyword=NULL)
            //回答信息模板
             $qu = D('Wxuser')->join('join tp_member on tp_wxuser.member_id=tp_member.member_id')->where(array('wxUid'=>$q['wxUserId']))->find();

            $val=array( 
                "keyword1"=>array('value'=>$u['nickname']),
                "keyword2"=>array('value'=>I('get.content')),
                "keyword3"=>array('value'=>date("Y-m-d H:i:s")),
                "keyword4"=>array('value'=>' 回答了你的问题（'.$q['appeal'].'）'),
            ); 
            $ret['tempRes'] = send_weapp_msg($qu['open_id'],'hYsf75JtVJYKd5N42EXznpzTAxXaYTGehagPYIuLEX4','wenzhang/wenzhang?id='.$q['qId'],$q['formid'],$val,'');
            $ret['result'] = 'ok';
        }catch(\Exception $e){
            $ret['ms'] = $e->getMessage();
        }
        echo json_encode($ret);
    }

    public function upLike(){

        try {
            $LikelogDb = D('Likelog');
            $map=array('aId' => I('aid'),'wxUid' => I('get.wxuId'));
            $likeLog = $LikelogDb->where($map)->find();
            $addQty=1;
            if ($likeLog != null) {
                if($likeLog['dlike']==1)
                    $addQty=-1;
                $likeLog['dlike'] = 1 - $likeLog['dlike'];
                $LikelogDb->where($map)->data($likeLog)->save();
                $ret['type'] = '1';
            } else {
                $data = array();
                $data['dlike'] = 1;
                $data['collection'] = 0;
                $data['aId'] = I('aid');
                $data['wxUid'] = I('get.wxuId');
                $LikelogDb->data($data)->add();
                $ret['type'] = '0';
            }

            $Answer=D('Answer');
            $ans=$Answer->where(array('aId'=>I('aid')))->find();
            $ans['likeQty']=$ans['likeQty']+$addQty;
            $Answer->where(array('aId'=>I('aid')))->data($ans)->save();

            $WxUserDb = D('Wxuser');
            $user = $WxUserDb->where(array('wxUid'=>I('get.wxuId')))->find();
            $user['likeQty']+=$addQty;
            $WxUserDb->where(array('wxUid'=>I('get.wxuId')))->data($user)->save();

            $ret['result'] = 'ok';
            echo json_encode($ret);
        }catch(\Exception $e){
            $ret['ms'] = $e->getMessage();
            echo json_encode($ret);
        }
    }

    public function upollection(){

        try {
            $LikelogDb = D('Likelog');
            $map=array('aId' => I('aid'),'wxUid' => I('get.wxuId'));
            $likeLog = $LikelogDb->where($map)->find();
            $addQty=1;
            if ($likeLog != null) {
                if($likeLog['collection']==1)
                    $addQty=-1;
                $likeLog['collection'] = 1 - $likeLog['collection'];
                $LikelogDb->where($map)->data($likeLog)->save();
                $ret['type'] = '1';
            } else {
                $data = array();
                $data['dlike'] = 0;
                $data['collection'] = 1;
                $data['aId'] = I('aid');
                $data['wxUid'] = I('get.wxuId');
                $LikelogDb->data($data)->add();
                $ret['type'] = '0';
            }

            $Answer=D('Answer');
            $ans=$Answer->where(array('aId'=>I('aid')))->find();
            $ans['collectionQty']=$ans['collectionQty']+$addQty;
            $Answer->where(array('aId'=>I('aid')))->data($ans)->save();

            $WxUserDb = D('Wxuser');
            $user = $WxUserDb->where(array('wxUid'=>I('get.wxuId')))->find();
            $user['collectionQty']+=$addQty;
            $WxUserDb->where(array('wxUid'=>I('get.wxuId')))->data($user)->save();

            $ret['result'] = 'ok';
            echo json_encode($ret);
        }catch(\Exception $e){
            $ret['ms'] = $e->getMessage();
            echo json_encode($ret);
        }
    }


    
}